package edu.ec.java02.group02.service.impl;

import edu.ec.java02.group02.controller.ProductController;
import edu.ec.java02.group02.domain.*;
import edu.ec.java02.group02.mapper.*;
import edu.ec.java02.group02.service.ManagerService;
import edu.ec.java02.group02.util.Page;
import edu.ec.java02.group02.util.StaticUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private ManagerInfoMapper managerInfoMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;

    private StaticUtil staticUtil = new StaticUtil();

    @Override
    public boolean login(ManagerInfo managerInfo) {
        ManagerInfo adminForCheck = managerInfoMapper.selectByAccount(managerInfo.getManagerName());
        if(adminForCheck != null){
            if(managerInfo.getManagerPassword().equals(adminForCheck.getManagerPassword())){
                return true;
            }
        }
        return false;
    }

    public boolean login(UserInfo userInfo) {
        UserInfo u= new UserInfo();
        u.setUserId(1);
//        获取到dao层的数据
        UserInfo u2=  userInfoMapper.getUserInfo(u);
        if(u2!=null&&u2.getUserPassword().equals(userInfo.getUserPassword())) {
            return true;
        }
        return false;
    }

    @Override
    public List<UserInfo> getUserList() {
        return userInfoMapper.selectAllUser();
    }

    @Override
    public List<UserInfo> selectUserListPage(Page page) {
        return userInfoMapper.selectUserPage(page);
    }

    @Override
    public Integer selectUserPageCount(Page page) {
        return userInfoMapper.selectUserPageCount(page);
    }

    @Override
    public UserInfo getUserById(int userId) {
        return userInfoMapper.selectUserById(userId);
    }

    @Override
    public String saveUserInfo(UserInfo userInfo) {
        return userInfoMapper.insertUser(userInfo)==1?staticUtil.success:staticUtil.fail;
    }

    @Override
    public String modifyUserInfo(UserInfo userInfo) {
        return userInfoMapper.updateUser(userInfo)==1?staticUtil.success:staticUtil.fail;
    }

    @Override
    public String modifyUserStatus(int userId,String userState) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        if("停用".equals(userState)){
            userInfo.setUserState("停用");
        }else if("启用".equals(userState)){
            userInfo.setUserState("启用");
        }
        return userInfoMapper.updateUserState(userInfo)==1?staticUtil.success:staticUtil.fail;
    }

    @Override
    public String deleteUserInfo(String userIds) {
        String userId[] = userIds.split(";");
        for(int i=0;i<userId.length;i++){
            if(userInfoMapper.deleteUser(Integer.parseInt(userId[i]))!=1){
                return staticUtil.fail;
            }
        }
        return staticUtil.success;
    }

    @Override
    public List<ProductInfo> getProductList() {
        return productInfoMapper.selectProductAll();
    }

    @Override
    public List<ProductInfo> selectProductListPage(Page page) {
        return productInfoMapper.selectProductPage(page);
    }

    @Override
    public Integer selectProductListPageCount(Page page) {
        return productInfoMapper.selectProductListPageCount(page);
    }

    @Override
    public ProductInfo getProductById(int productId) {
        return productInfoMapper.selectProductById(productId);
    }

    @Override
    public int saveProduct(ProductInfo productInfo,MultipartFile productImage,String url) {
        if(!"".equals(productImage.getOriginalFilename())||productImage.getOriginalFilename() != ""){
            //将文件名分为前缀pre和后缀sufix
            String pre = productImage.getOriginalFilename().substring(0,productImage.getOriginalFilename().lastIndexOf("."));
            String sufix = productImage.getOriginalFilename().substring(productImage.getOriginalFilename().lastIndexOf("."));
            String date= new Date().toString();
            date=date.replace(" ", "_").replace(":", "_");
            File toFile = new File(url + "/" + date + pre + sufix);
            try {
                productInfo.setProductImage("/" + date + pre + sufix);
                productImage.transferTo(toFile);
            } catch (IOException ex) {
                Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            productInfo.setProductImage("/none.jpg");
        }
        return productInfoMapper.insertProduct(productInfo);
    }

    @Override
    public String modifyProductStatus(int productId,String productState) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId(productId);
        if("上架".equals(productState)){
            productInfo.setProductState("1");
        }else if("下架".equals(productState)){
            productInfo.setProductState("0");
        }
        return productInfoMapper.updateProductState(productInfo)==1?staticUtil.success:staticUtil.fail;
    }

    @Override
    public int modifyProductInfo(ProductInfo productInfo,MultipartFile productImage,String url) {
        if(!"".equals(productImage.getOriginalFilename())||productImage.getOriginalFilename() != ""){
            //将文件名分为前缀pre和后缀sufix
            String pre = productImage.getOriginalFilename().substring(0,productImage.getOriginalFilename().lastIndexOf("."));
            String sufix = productImage.getOriginalFilename().substring(productImage.getOriginalFilename().lastIndexOf("."));
            String date= new Date().toString();
            date=date.replace(" ", "_").replace(":", "_");
            File toFile = new File(url + "/" + date + pre + sufix);
            try {
                productInfo.setProductImage("/" + date + pre + sufix);
                productImage.transferTo(toFile);
            } catch (IOException ex) {
                Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalStateException ex) {
                Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
           return productInfoMapper.updateProductNoImage(productInfo);
        }
        return productInfoMapper.updateProductNoImage(productInfo);
    }

    @Override
    public String delProductInfo(String productIds) {
        String productId[] = productIds.split(";");
        for(int i=0;i<productId.length;i++){
            if(productInfoMapper.deleteProduct(Integer.parseInt(productId[i]))!=1){
                return staticUtil.fail;
            }
        }
        return staticUtil.success;
    }

    @Override
    public List<Order> getOrderList() {
        return orderMapper.selectAllOrder();
    }

    @Override
    public List<Order> selectOrderListPage(Page page) {
        return orderMapper.selectOrderPage(page);
    }

    @Override
    public Integer selectOrderPageCount(Page page) {
        return orderMapper.selectOrderPageCount(page);
    }

    @Override
    public List<OrderDetail> getOrderDetailList() {
        return orderDetailMapper.selectAllDetailOrder();
    }

    @Override
    public List<OrderDetail> selectOrderDetailListPage(Page page) {
        return orderDetailMapper.selectDetailOrderPage(page);
    }

    @Override
    public Integer selectOrderDetailPageCount(Page page) {
        return orderDetailMapper.selectOrderDetailPageCount(page);
    }

    @Override
    public OrderDetail getOrderDetailByOrderDetailId(int orderDetailId) {
        return orderDetailMapper.selectDetailOrderById(orderDetailId);
    }

    @Override
    public List<Order> ListOrderInfo() {
        return null;
    }

    @Override
    public String modifyOrderDetailInfo(OrderDetail orderDetail) {
        return null;
    }

//    @Override
//    public String modifyOrderDetailInfo(String orderNo) {
//        return null;
//    }

    @Override
    public String deleteOrder(String orderIds,String orderNos) {
        String orderId[] = orderIds.split(";");
        String orderNo[] = orderNos.split(";");
        for(int i=0;i<orderId.length;i++){
            if(orderMapper.deleteOrder(Integer.parseInt(orderId[i]))!=1){
                return staticUtil.fail;
            }
        }
        for(int j=0;j<orderNo.length;j++){
            if(orderDetailMapper.deleteDetailOrderByNo(orderNo[j])!=1){
                return staticUtil.fail;
            }
        }
        return staticUtil.success;
    }

    @Override
    public String deleteOrderDetail(int orderDetailId) {
        return orderDetailMapper.deleteDetailOrderById(orderDetailId)==1?staticUtil.success:staticUtil.fail;
    }

    @Override
    public Order findOrderInfo(String orderNo) {
        return null;
    }

    @Override
    public String saveOrderInfo(OrderDetail orderDetail) {
        Date date = new Date();
        String orderNo = UUID.randomUUID().toString().replace("-","");
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setOrderStatus("未支付");
        orderDetail.setOrderNo(orderNo);
        if(orderMapper.insertOrder(order)==1){
            ProductInfo productInfo = productInfoMapper.selectProductById(orderDetail.getProductId());
            orderDetail.setProductName(productInfo.getProductName());
            orderDetail.setProductTotalprice(productInfo.getProductPrice()*orderDetail.getProductNumber());
            return orderDetailMapper.insertDetailOrder(orderDetail)==1?staticUtil.success:staticUtil.fail;
        }
        return staticUtil.fail;
    }

   /* @Override
    public String excelOrderInfo(String orderDetailId,String pathFile) {
        List<OrderDetail> list = new ArrayList<OrderDetail>();
        list.add(orderDetailMapper.selectDetailOrderById(Integer.parseInt(orderDetailId)));
       String s = new ExcelUtil().ExportDataToOrderExcel(pathFile,list);
        return s;
    }*/
//        List<OrderDetail> list = new ArrayList<OrderDetail>();
//        list.add(orderDetailMapper.selectDetailOrderById(Integer.parseInt(orderDetailId)));
//        String s = new ExcelUtil().ExportDataToOrderExcel(pathFile,list);
        //return null;
    }
